package com.example.carrierapp.ui.person.notice;

import android.app.Activity;
import android.content.Context;

import com.example.carrierapp.base.BasePresenter;
import com.example.carrierapp.bean.home.NoticeDetailBean;
import com.example.carrierapp.bean.home.NoticeListBean;
import com.example.carrierapp.net.ApiMiddleHelper;
import com.example.carrierapp.net.listener.DataCallBackListener;
import com.example.carrierapp.util.LogUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Flowable;

public class NoticePresenter extends BasePresenter<NoticeContract.View> implements NoticeContract.Presenter {
    private Context mContext;
    public NoticePresenter(Activity context) {
        mContext=context;
    }

    @Override
    public void start() {

    }

    @Override
    public void getNoticeList(int skipCount) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("maxResultCount", "10");
        biddingMap.put("skipCount", skipCount + "");
        biddingMap.put("carTypeName", 4 + "");
        biddingMap.put("machineType", 7 + "");
        Flowable<JsonObject> flowable = apiStrores.getNoticeList(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("getNoticeList-onSuccess-" + json);
                NoticeListBean bean = new Gson().fromJson(json, new TypeToken<NoticeListBean>() {
                }.getType());
                if (getView() != null) {
                getView().getNoticeListSuccess(bean);

                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getNoticeList-onFailure-" + msg);
                if (getView() != null) {

                getView().getNoticeListFailde(msg);

                }
            }
        });
    }

    @Override
    public void getNoticeDetail(int id) {
        Map<String, String> biddingMap = new HashMap<>();
        biddingMap.put("id", id + "");
        Flowable<JsonObject> flowable = apiStrores.getNoticeDetail(biddingMap);

        ApiMiddleHelper.getInstance().toNetIo2Main(mContext,flowable, new DataCallBackListener() {
            @Override
            public void onSuccess(String json) {
                LogUtil.d("getNoticeDetail-onSuccess-" + json);
                NoticeDetailBean bean = new Gson().fromJson(json, new TypeToken<NoticeDetailBean>() {
                }.getType());
                if (getView() != null) {
                getView().getNoticeDetailSuccess(bean);

                }
            }

            @Override
            public void onFailure(String msg) {
                LogUtil.d("getNoticeDetail-onFailure-" + msg);
                if (getView() != null) {
                getView().getNoticeDetailFailed(msg);

                }
            }
        });
    }
}
